#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(width(),height());

    player = new QMediaPlayer(this);
    playList = new QMediaPlaylist(this);
    playList->setPlaybackMode(QMediaPlaylist::Loop);
    player->setPlaylist(playList);

    connect(player,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(onStateChanged(QMediaPlayer::State)));
    connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(onPstChanged(qint64)));
    connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(onDrtChanged(qint64)));
    connect(playList,SIGNAL(currentIndexChanged(int)),this,SLOT(onPlayerListChanged(int)));


}

MainWindow::~MainWindow()
{
    delete ui;
}

//播放按钮切换状态
void MainWindow::onStateChanged(QMediaPlayer::State state)
{
    //根据媒体播放器是否处于播放状态，设置播放按钮是否可点击
    ui->pushButton_play->setEnabled(!(state == QMediaPlayer::PlayingState));
    ui->pushButton_pause->setEnabled(state == QMediaPlayer::PlayingState);
    ui->pushButton_terminate->setEnabled(!(state == QMediaPlayer::PausedState));

}

//播放列表
void MainWindow::onPlayerListChanged(int pos)
{
    //点击播放之后，程序会将歌曲名称的文本显示在label里面
    ui->listWidget->setCurrentRow(pos);
    QListWidgetItem *item = ui->listWidget->currentItem();
    if(item)
    {
        ui->label_singleName->setText(item->text());
    }
}

 //歌曲总时长
void MainWindow::onDrtChanged(qint64 drt)
{
    ui->horizontalSlider_progress->setMaximum(drt);

    //为什么 drt/1000 能得到秒？
    int sec = drt/1000;//秒
    int min = sec/60;//分
    sec = sec % 60;//余数为秒

    drtTime = QString::asprintf("%d:%d",min,sec);
    ui->label_playTime->setText(pstTime + "|" + drtTime);
}


//播放歌曲当前位置
void MainWindow::onPstChanged(qint64 pos)
{
    // 如果用户正在拖动进度条，则不更新进度显示，避免干扰用户操作
    if(ui->horizontalSlider_progress->isSliderDown())
    {
        return;
    }

    int sec = pos / 1000;//秒
    int min = sec / 60;//分
    sec = sec%60;//余数为秒

    ui->horizontalSlider_progress->setSliderPosition(pos);

    pstTime = QString::asprintf("%d:%d",min,sec);
    ui->label_playTime->setText(pstTime + "|" + drtTime);

}

//打开文件
void MainWindow::on_pushButton_open_clicked()
{
    //获取当前目录
    QString currentPath = QDir::homePath();
    QString dialogTitle = "请选择音频文件:";
    QString strFilter = "所有文件(*.*);;音频文件(*.mp3 *.wav);;mp3文件(*.mp3);;WAV文件(*.wav)";
    QStringList fileList = QFileDialog::getOpenFileNames(this,dialogTitle,currentPath,strFilter);
    if(fileList.count() < 1)
    {
        return;
    }

    for(int i=0;i<fileList.count();i++)
    {
        QString file = fileList.at(i);
        playList->addMedia(QUrl::fromLocalFile(file));//添加文件

        QFileInfo fileInfo(file);
        ui->listWidget->addItem(fileInfo.fileName());//将文件名添加到界面listWidget控件
    }

    if(player->state() != QMediaPlayer::PlayingState)
    {
        playList->setCurrentIndex(0);
    }
    player->play();
}

void MainWindow::on_pushButton_play_clicked()
{
    if(playList->currentIndex() < 0)
    {
        playList->setCurrentIndex(0);
    }

    player->play();
}

void MainWindow::on_pushButton_pause_clicked()
{
    player->pause();
}

void MainWindow::on_pushButton_terminate_clicked()
{
    player->stop();
}

void MainWindow::on_pushButton_privous_clicked()
{
    playList->previous();
}

void MainWindow::on_pushButton_next_clicked()
{
    playList->next();
}


//控制静音状态
void MainWindow::on_pushButton_volumn_clicked()
{
    bool isMuted  = player->isMuted();

    //开启媒体播放器的静音状态
    player->setMuted(!isMuted);

    if(isMuted )
    {
        ui->pushButton_volumn->setIcon(QIcon(":/images/volumn.bmp"));
    }
    else
    {
        ui->pushButton_volumn->setIcon(QIcon(":/images/mute.bmp"));
    }
}


//控制音量大小
void MainWindow::on_horizontalSlider_volumn_valueChanged(int value)
{
    player->setVolume(value);
}


//控制进度条位置
void MainWindow::on_horizontalSlider_progress_valueChanged(int value)
{
    player->setPosition(value);
}
